package com.kaifamiao.servlet;

import org.tinylog.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/session/attribute/*")
public class SessionAttributeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String attrName = request.getParameter("attr");
        HttpSession session = request.getSession();
        session.removeAttribute( attrName );
        response.sendRedirect( request.getContextPath() + "/attribute.jsp" );
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String attrName = request.getParameter( "name" );
        String attrValue = request.getParameter( "value" );
        HttpSession session = request.getSession();
        session.setAttribute( attrName, attrValue );
        response.sendRedirect( request.getContextPath() + "/attribute.jsp" );
    }
}
